package com.quectel.sdk.hikisc.entranceguard;

import com.fasterxml.jackson.databind.JsonNode;
import com.quectel.sdk.hikisc.AbstractHikBaseClient;
import com.quectel.sdk.hikisc.entranceguard.model.SyncGuardResp;
import com.quectel.sdk.hikisc.monitor.MonitorClient;
import com.quectel.util.common.JacksonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 在这里编写说明
 *
 * @author: rananxin
 * @email: ryan.ran@quectel.com
 * @date: 2022-10-19 11:01:00
 */
public class EntranceGuardClient extends AbstractHikBaseClient {

    private static final String SUCCESS_CODE = "0";
    private static final Logger LOGGER = LoggerFactory.getLogger(MonitorClient.class);

    /**
     * 获取监控点资源列表
     * 涉及的api包括：
     * 分页获取监控点资源 path:/api/resource/v1/cameras
     *
     * @return 全部监控点资源集合
     */
    public List<SyncGuardResp> drawResources() {
        final int fetchSize = 999;
        Map<String, Object> params = new HashMap<>();
        params.put("pageNo", 1);
        params.put("pageSize", fetchSize);
        params.put("resourceType", "door");

        final String path = "/api/irds/v2/deviceResource/resources";

        List<SyncGuardResp> result = new ArrayList<>();
        while (true) {
            String response = requester.post(path, params);
            JsonNode respJson = JacksonUtils.parse(response);
            String code = respJson.get("code").asText();
            if (!SUCCESS_CODE.equals(code)) {
                LOGGER.error("海康 获取门禁资源异常 response {}", response);
                break;
            }
            JsonNode value = respJson.findValue("data").findValue("list");
            result.addAll(JacksonUtils.parseArray(JacksonUtils.toJsonString(value), SyncGuardResp.class));
            if (value.size() < fetchSize) {
                break;
            }
        }
        LOGGER.debug("海康 获取门禁资源成功 total {}", result.size());
        return result;
    }

}
